//
//  GGNewFeatureViewController.swift
//  SwiftWeico
//
//  Created by denjunjie1 on 15/12/17.
//  Copyright © 2015年 Gel. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class GGNewFeatureViewController: UICollectionViewController {
    
    private let layout = UICollectionViewFlowLayout()
    
    init(){
    
        super.init(collectionViewLayout: layout)
        
        
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView!.registerClass(GGNewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        prepareCollectionView()

        
    }
    
    private func prepareCollectionView(){
    
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
        
    }

    private let ItemCount = 4
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return ItemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GGNewFeatureViewCell
    
        // Configure the cell
        
        cell.index = indexPath.row
        
        return cell
    }

    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        

        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last
        
        if visibleIndexPath?.item == ItemCount - 1 {
        
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath!) as! GGNewFeatureViewCell
       
            cell.startBtnAnimate()
        }
        
        
    }
}


class GGNewFeatureViewCell : UICollectionViewCell {
    
    var index = 0 {
    
        didSet {
            bkgImage.image = UIImage(named: "new_feature_\(index+1)")
            
            startBtn.hidden = true
           
        }
    
    }
    
    
     func startBtnAnimate(){
         startBtn.hidden = false
     startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
                self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
            
            }) { (_) -> Void in
                print("开始按钮动画完成")
                
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    
    private func prepareUI(){
    
        
        contentView.addSubview(bkgImage)
        contentView.addSubview(startBtn)
        
        
        bkgImage.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: bkgImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    
    }
    
    
    private lazy var bkgImage : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn : UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setTitle("Start NewFeature", forState: UIControlState.Normal)
        
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        
        
        return btn
        
    }()
  
    
}